package com.tianyuan.dplrf.wxapi;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.pm.PackageManager;

import androidx.annotation.NonNull;

import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tianyuan.dplrf.app.AppApplication;

public class PlatConfig {
    AppApplication appApplication;

    public PlatConfig(AppApplication appApplication) {
        this.appApplication = appApplication;
    }

    public void shareText(String text){
        WXTextObject textObj = new WXTextObject();
        textObj.text = text;

//用 WXTextObject 对象初始化一个 WXMediaMessage 对象
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = textObj;
        msg.description = text;

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());  //transaction字段用与唯一标示一个请求
        req.message = msg;
//        req.scene = mTargetScene;
        req.scene = SendMessageToWX.Req.WXSceneSession;

//调用api接口，发送数据到微信
//        appApplication.getApi().sendReq(req);


    }

    public static boolean isAppInstalled(Context context) {
        return isAppInstalled(context, "com.tencent.mm");
    }

    private static boolean isAppInstalled(Context context, @NonNull String packageName) {
        try {
            context.getPackageManager().getApplicationInfo(packageName, 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

    public interface OnShareListener {

        /**
         * 分享开始
         *
         */
        default void onStart() {}

        /**
         * 分享成功的回调
         *
         */
        void onSucceed();

        /**
         * 分享失败的回调
         *
         * @param t             错误原因
         */
        default void onError(Throwable t) {}

        /**
         * 分享取消的回调
         *
         */
        default void onCancel() {}
    }
}
